thin.imports('/public/home/css/global.project.css');

thin.imports('/public/home/configure/conf.minfo.js');
thin.imports('/public/modules/upload.js');

thin.use(function() {
	var upload = thin.uploadFiles;

	$('[layout-mark=uploadFile]').on('change', function(e) {
		var picture = upload.getPicture(e);
		if(picture) {
			$(this).parent().children('img').attr('src', picture);
			var u = $(this).attr('data-u');
			var fileInput = this.files[0];
			var form = new FormData();
			form.append("file", fileInput);
			form.append("name", "file");
			form.append("dir", 'upload');
			upload.getSrc('/api?common.upload', form, window[u]);
			this.parentNode.reset();
		}
	});

	$('[layout-mark=tab-item]').off('click').on('click', function(e) {
		var e = e || window.event;
		e.stopPropagation();
		var that = $(this),
			itor = that.attr('data-itor');
		that.addClass('weui-bar__item--on').siblings('a').removeClass('weui-bar__item--on');
		$(itor).addClass('weui-tab__bd-item--active').siblings('div').removeClass('weui-tab__bd-item--active');
		checkTab(itor);
		return false;
	});

	$('#msg-modal-list').on('click', '#btn-msgtzz', function() {
		var that = $(this),
			_id = that.attr('data-id'),
			proid = that.attr('data-proid'),
			isread = that.attr('data-isread'),
			ischat = that.attr('data-ischat');
		var actions_opt = [];
		actions_opt.push({
			text: "查看",
			onClick: function() {
				if(isread == 0) {
					$.showLoading();
					__getdata_msgread['id'] = _id;
					factory.request('_msg_read', function() {
						window.location.href = '/mobile/chain/project?proid=' + proid + '&msgid=' + _id + '&isPull=1#details';
					});
				} else {
					window.location.href = '/mobile/chain/project?proid=' + proid + '&msgid=' + _id + '&ischat=true&isPull=1#details';
				}
			}
		});
		if(ischat == 0) {
			actions_opt.push({
				text: "约谈",
				onClick: function() {
					$.showLoading();
					__getdata_msgchat['id'] = _id;
					factory.request('_msg_chat');
				}
			});
		}
		actions_opt.push({
			text: "拒绝",
			onClick: function() {
				$.showLoading();
				__getdata_msgrefuse['id'] = _id;
				factory.request('_msg_refuse', _id);
			}
		});
		$.actions({
			actions: actions_opt
		});
	}).on('click', '#btn-msgcyz', function() {
		var that = $(this),
			_id = that.attr('data-id'),
			proid = that.attr('data-proid'),
			isread = that.attr('data-isread');
		$.actions({
			actions: [{
				text: "查看",
				onClick: function() {
					window.location.href = '/mobile/chain/project?proid=' + proid + '#details';
				}
			}, {
				text: "撤回",
				onClick: function() {
					$.showLoading();
					__getdata_msgundo['id'] = _id;
					factory.request('_msg_undo', _id);
				}
			}]
		});
	});

	$('#btn-memberinfo').on('click', function() {
		submitMemberInfo();
	});

	$('#btn-memberlike').on('click', function(e) {
		submitMemberLike();
	});

	$('#submit-price').on('click', function(e) {
		var n_n = $('#ipt-ml-begin_money').val(),
			n_p = $('#ipt-ml-end_money').val();
		(n_n > 0 && n_p > 0) && $('#btn-edit-price').find('.value-text').html(n_n + '-' + n_p + '万元');
		$.closePopup();
	});

	$('#submit-udpass').on('click', submitUdpass);
	$('#submit-repass').on('click', submitRepass);

	// 路由
	checkLocation();
});

var __getdata_pwdupdate = {},
	__getdata_pwdreset = {};

function submitUdpass() {
	__getdata_pwdupdate = {
		'passport': $('#ipt-ud-passport').val(),
		'password': $('#ipt-ud-password').val(),
		'confpwd': $('#ipt-ud-confpwd').val()
	};
	if(__getdata_pwdupdate['passport'] == '') {
		$.toptip('请输入原密码', 'error');
		return;
	} else if(__getdata_pwdupdate['password'] == '') {
		$.toptip('请输入新密码', 'error');
		return;
	} else if(__getdata_pwdupdate['confpwd'] == '') {
		$.toptip('再次输入新密码', 'error');
		return;
	}
	$.showLoading();
	factory.request('_member_pwdupdate');
}

function submitRepass() {
	var data_opt = {
		'mobile': $('#ipt-re-mobile').val(),
		'code': $('#ipt-re-code').val(),
		'password': $('#ipt-re-password').val(),
		'confpwd': $('#ipt-re-confpwd').val()
	}
	if(data_opt['mobile'] == '') {
		$.toptip('请输入手机号', 'error');
		return;
	} else if(data_opt['code'] == '') {
		$.toptip('请输入验证码', 'error');
		return;
	} else if(data_opt['password'] == '') {
		$.toptip('请输入新密码', 'error');
		return;
	} else if(data_opt['confpwd'] == '') {
		$.toptip('再次输入新密码', 'error');
		return;
	}
	__getdata_pwdreset = data_opt
	verifySMSCode(function() {
		factory.request('_member_pwdreset');
	});
}

function __complete_pwdupdate(res) {
	$.hideLoading();
	if(res.status == 201) {
		$.toast("修改成功", 960);
		setTimeout(function() {
			checkLocation('member');
		}, 800);
		$('#ipt-re-passport').val('');
		$('#ipt-re-password').val('');
		$('#ipt-re-confpwd').val('');
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_pwdreset(res) {
	$.hideLoading();
	if(res.status == 201) {
		$.toast("修改成功", 960);
		setTimeout(function() {
			checkLocation('member');
		}, 800);
		$('#ipt-ud-code').val('');
		$('#ipt-ud-password').val('');
		$('#ipt-ud-confpwd').val('');
	} else {
		$.toptip(res.error, 'error');
	}
}

var __getdata_msgread = {},
	__getdata_msgundo = {},
	__getdata_msgchat = {},
	__getdata_msgrefuse = {};

function __complete_msgread(res, unkn) {
	$.hideLoading();
	if(res.status == 201) {
		unkn();
	} else {

	}
}

function __complete_msgchat(res) {
	$.hideLoading();
	if(res.status == 201) {
		jude_msgpage = false;
		$('#msg-modal-list').html('');
		checkTab('msg-modal');
	} else {

	}
}

function __complete_msgundo(res, unkn) {
	$.hideLoading();
	if(res.status == 204) {
		$('.msgpanel[data-id=' + unkn + ']').remove();
	}
}

function __complete_msgrefuse(res, unkn) {
	$.hideLoading();
	if(res.status == 204) {
		$('.msgpanel[data-id=' + unkn + ']').remove();
	}
}

var __uheadimgurl = {
	'onload': function(res) {
		if(res.status == 201) {
			$('#ipt-mi-headimgurl').val(res.data.uri);
		} else {
			$.toptip(res.error, 'error');
		}
	},
	'onerror': function(res) {
		$.toptip('上传失败，请检测图片和网络', 'error');
	}
}

var __getdata_minfoedit = {},
	__getdata_mlikeedit = {};

function submitMemberInfo() {
	$('[id*=ipt-mi]').each(function(index) {
		var that = $(this),
			name_arr = that.attr('id'),
			name = name_arr.split('-')[2];
		__getdata_minfoedit[name] = that.val();
	});
	$.showLoading();
	factory.request('_minfo_edit');
}

function submitMemberLike() {
	$('[id*=ipt-ml]').each(function(index) {
		var that = $(this),
			name_arr = that.attr('id'),
			name = name_arr.split('-')[2];
		__getdata_mlikeedit[name] = that.val();
	});
	$.showLoading();
	factory.request('_mlike_edit');
}

var __complete_minfoedit = __complete_mlikeedit = function(res) {
	$.hideLoading();
	if(res.status == 201) {
		jude_mbinfo = false;
		$.toast("编辑成功", 960);
		setTimeout(function() {
			checkLocation('member');
		}, 800);
	} else {
		$.toptip(res.error, 'error');
	}
}

var jude_minfo = false,
	jude_mlike = false,
	jude_cityPicker = false,
	jude_mbinfo = false;

function __complete_macc(res) {
	$.hideLoading();
	if(res.status == 200) {
		$('#acc-price').text(res.data);
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_minfoget(res) {
	$.hideLoading();
	if(res.status == 200) {
		jude_minfo = true;
		var data = res.data,
			province = '',
			city = '',
			headimgurl = '';
		for(var i in data) {
			$('#ipt-mi-' + i).val(data[i]);
			(i == 'province') && (province = data[i]);
			(i == 'city') && (city = data[i]);
			(i == 'headimgurl') && (headimgurl = data[i]);
		}
		$('#ipt-mobile').val(data.mobile);
		(headimgurl != '') && $('#btn-headimgurl').find('img').attr('src', headimgurl);
		$('#btn-city').find('.value-text').html(province + '  ' + city);
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_mlikeget(res) {
	setTimeout(function() {
		$.hideLoading();
	}, 1200);
	var round = '',
		industry = '',
		city = '';
	if(res.status == 200) {
		jude_minfo = true;
		var data = res.data,
			begin_money = '',
			end_money = '';
		for(var i in data) {
			$('#ipt-ml-' + i).val(data[i]);
			$('#btn-ml-' + i).find('.value-text').html(data[i]);
			(i == 'round') && (round = data[i]);
			(i == 'industry') && (industry = data[i]);
			(i == 'city') && (city = data[i]);
			(i == 'begin_money') && (begin_money = data[i]);
			(i == 'end_money') && (end_money = data[i]);
		}
		(begin_money != '' && end_money != '') && $('#btn-edit-price').find('.value-text').html(begin_money + '-' + end_money + '万元');
	} else {
		$.toptip(res.error, 'error');
	}
	__getdata_opts.name = 'round';
	factory.request('_common_opts', ['round', round]);
	__getdata_opts.name = 'industry';
	factory.request('_common_opts', ['industry', industry]);
	__getdata_opts.name = 'city';
	factory.request('_common_opts', ['city', city]);
}

var __getdata_opts = {},
	__getdata_minfobase = {};

var data_mobile = '';

function __complete_minfobase(res) {
	$.hideLoading();
	if(res.status == 200) {
		var role;
		if(res.data.role == 1) {
			role = '创业者';
		} else if(res.data.role == 2) {
			role = '个人天使投资者';
		} else if(res.data.role == 3) {
			role = '机构投资者';
		}
		$('#mbinfo').html(res.data.info.city + '  ' + role + '<br />' + res.data.info.company);
		data_mobile = res.data.info.mobile;
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_opts(res, unkn) {
	if(res.status == 200) {
		$('#btn-ml-' + unkn[0]).on('click', function(e) {
			var e = e || window.event;
			e.stopPropagation();
			e.preventDefault();
			$(this).select('open');
			return false;
		}).select({
			title: "请选择",
			multi: true,
			items: res.data,
			input: unkn[1],
			onClose: function(res) {
				$('#btn-ml-' + unkn[0]).find('.value-text').html(res.data.titles);
				$('#ipt-ml-' + unkn[0]).val(res.data.titles);
			}
		});
	} else {
		// nothing
	}
}

function __complete_msgvolist(res) {
	$.hideLoading();
	if(res.status == 200) {
		if(res.data.list.length > 0) {
			createMsgList(res.data);
		} else {
			$('#msg-modal-list').parent().addClass('nothing-bg');
		}
	} else {
		$('#msg-modal-list').parent().addClass('nothing-bg');
	}
}

function createMsgList(data) {
	var ele = '';
	var _role = tool.__cookie.get('memberRole');
	for(var i in data.list) {
		var list = data.list[i];
		ele += '<div class="msgpanel" data-id="' + list.id + '">';
		//ele += '<p class="fs14r text-center p-y-1x weui-line-reset">' + tool.formatDate('yy-mm-dd hh:ii:ss', list.create_time) + '</p>';
		if(_role >= 2) {
			// 投资人
			ele += '<div class="weui-flex text-center p-y-1x weui-line-reset">';
			ele += '<div class="weui-flex__item">';
			ele += '<img src="' + list.from_member_headimgurl + '" class="pic-circle" />';
			ele += '<p class="m-t-1x fs12r">' + list.from_member_nickname + '</p>';
			ele += '</div>';
			ele += '<div class="weui-flex__item">';
			if(list.is_read == 0) {
				// dcdcdc (未查阅)
				ele += '<div class="p-t-2x c-dcdcdc">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">未查阅</p>';
			} else if(list.is_chat == 0) {
				// e64340 (未约谈)
				ele += '<div class="p-t-2x c-e64340">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">未约谈</p>';
			} else {
				// 1aad19 (已约谈)
				ele += '<div class="p-t-2x c-1aad19">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">已约谈</p>';
			}
			ele += '<p class="fs12r">' + tool.formatDate('yy-mm-dd', list.create_time) + '</p>';
			ele += '</div>';
			ele += '</div>';
			ele += '<div class="weui-flex__item">';
			ele += '<img src="' + list.project.logo + '" class="pic-circle" />';
			ele += '<p class="m-t-1x fs12r">' + list.project.title + '</p>';
			ele += '</div>';
			ele += '</div>';
		} else {
			// 创业者
			ele += '<div class="weui-flex text-center p-y-1x weui-line-reset">';
			ele += '<div class="weui-flex__item">';
			ele += '<img src="' + list.project.logo + '" class="pic-circle" />';
			ele += '<p class="m-t-1x fs12r">' + list.project.title + '</p>';
			ele += '</div>';
			ele += '<div class="weui-flex__item">';
			if(list.is_read == 0) {
				// dcdcdc (未查阅)
				ele += '<div class="p-t-2x c-dcdcdc">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">未查阅</p>';
			} else if(list.is_chat == 0) {
				// e64340 (未约谈)
				ele += '<div class="p-t-2x c-e64340">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">未约谈</p>';
			} else {
				// 1aad19 (已约谈)
				ele += '<div class="p-t-2x c-1aad19">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">已约谈</p>';
			}
			ele += '<p class="fs12r">' + tool.formatDate('yy-mm-dd', list.create_time) + '</p>';
			ele += '</div>';
			ele += '</div>';
			ele += '<div class="weui-flex__item">';
			ele += '<img src="' + list.to_member_headimgurl + '" class="pic-circle" />';
			ele += '<p class="m-t-1x fs12r">' + list.to_member_nickname + '</p>';
			ele += '</div>';
			ele += '</div>';
		}

		if(_role >= 2) {
			// 投资人
			ele += '<div class="weui-flex text-center">';
			ele += '<div class="weui-flex__item p-y-2x b-line-r">';
			if(list.is_chat == 1) {
				ele += '<a href="tel:' + list.from_mobile + '" class="f-mj"><i class="icon-phone p-r-1x fs24r"></i>' + list.from_mobile + '</a>';
			}
			ele += '</div>';
			ele += '<div class="p-y-2x pw32 color-wx">';
			ele += '<a href="javascript:;" data-id="' + list.id + '" data-proid="' + list.project_id + '" id="btn-msgtzz" data-isread="' + list.is_read + '" data-ischat="' + list.is_chat + '">操作</a>';
			ele += '</div>';
			ele += '</div>';
		} else {
			// 创业者
			ele += '<div class="weui-flex text-center">';
			ele += '<div class="weui-flex__item p-y-2x b-line-r">';
			if(list.is_chat == 1) {
				ele += '<a href="tel:' + list.to_mobile + '" class="f-mj"><i class="icon-phone p-r-1x fs24r"></i>' + list.to_mobile + '</a>';
			}
			ele += '</div>';
			ele += '<div class="p-y-2x pw32 color-wx">';
			ele += '<a href="javascript:;" data-id="' + list.id + '" data-proid="' + list.project_id + '" id="btn-msgcyz" data-isread="' + list.is_read + '" data-ischat="' + list.is_chat + '">操作</a>';
			ele += '</div>';
			ele += '</div>';
		}

		ele += '</div>';
	}
	$('#msg-modal-list').append(ele);
}

var jude_memsec = false,
	jude_bpvolist = false;

// 路由
function checkLocationCallback(hash) {
	if(hash == 'member') {
		if(!jude_mbinfo) {
			$.showLoading();
			__getdata_minfobase['model'] = 1;
			factory.request('_minfo_base');
			jude_mbinfo = true;
		}
	}

	if(/acc$/gi.test(hash)) {
		$.showLoading();
		factory.request('_m_acc');
	}

	if(/memberinfo/gi.test(hash)) {
		if(!jude_minfo) {
			$.showLoading();
			factory.request('_minfo_get');
		}
		if(!jude_cityPicker) {
			thin.imports('/public/plug/JQueryWeui/city-picker.min.js', function() {
				$("#btn-city").on('click', function(e) {
					var e = e || window.event;
					e.stopPropagation();
					e.preventDefault();
					$("#btn-city").picker('open');
					return false;
				}).cityPicker({
					title: '选择城市',
					showDistrict: false,
					onClose: function(res) {
						var d = res.displayValue;
						$('#ipt-mi-province').val(d[0]);
						$('#ipt-mi-city').val(d[1]);
						$('#btn-city').find('.value-text').html(d[0] + '  ' + d[1]);
					}
				});
				jude_cityPicker = true;
			});
		}
	}

	if(/memberlike/gi.test(hash)) {
		if(!jude_mlike) {
			$.showLoading();
			thin.imports('/public/home/configure/conf.opts.js');
			factory.request('_mlike_get');
			jude_mlike = true;
		}
	}

	if(hash == 'msg-modal') {
		//checkTab(hash);
		$('#member-template').show();
		$('[data-itor=#msg-modal]').trigger('click');
	}

	if(hash == 'memsec') {
		if(!jude_memsec) {
			$.showLoading();
			//thin.imports('/public/home/configure/conf.sms.js');
			thin.imports('/public/home/resource/sms.js');
			thin.imports('/public/home/configure/conf.pwd.js');
			setTimeout(function() {
				$.hideLoading();
			}, 1200);
			jude_memsec = true;
		}
	}

	if(hash == 'repass') {
		$('#ipt-re-mobile').val(data_mobile);
	}
}

var jude_msgpage = false;

function checkTab(itor) {
	if(/msg-modal/gi.test(itor)) {
		if(!jude_msgpage) {
			$.showLoading();
			thin.imports('/public/home/configure/conf.msg.js', function() {
				factory.request('_msg_volist');
			});
			jude_msgpage = true;
		}
	}
}